﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ThuChi
{
    public partial class QuanLySoThuChi : System.Web.UI.Page
    {
        int dem1, dem2;
        private void AddYearList()
        {
            if (cboNam.Items.Count == 0)
            {
                int maxYear = DateTime.Today.Year;
                int minYear = maxYear - 10;

                for (int i = maxYear; i >= minYear; i--)
                {
                    cboNam.Items.Add(i.ToString());
                    cboNamGiaoDich.Items.Add(i.ToString());
                }
            }
        }

        private void AddDayList()
        {
            int year = Convert.ToInt32(Session["Nam"].ToString());
            cboNgay.Items.Clear();
            cboNgay.Items.Add("");
            int month = 0;
            try
            {
                month = Convert.ToInt32(Session["Thang"].ToString());
                int maxDay = DateTime.DaysInMonth(year, month);

                for (int i = 1; i <= maxDay; i++)
                {
                    cboNgay.Items.Add(String.Format("{0:00}", i));
                }
                cboNgay.SelectedValue = Session["Ngay"].ToString();
            }
            catch
            {
                cboNgay.SelectedIndex = 0;
                Session["Ngay"] = "";
                grvSoThuChi.DataBind();
            }
        }

        private void AddDayGiaoDichList()
        {
            int year = Convert.ToInt32(Session["NamGiaoDich"].ToString());
            cboNgayGiaoDich.Items.Clear();
            int month = 0;
            try
            {
                month = Convert.ToInt32(Session["ThangGiaoDich"].ToString());
                int maxDay = DateTime.DaysInMonth(year, month);

                for (int i = 1; i <= maxDay; i++)
                {
                    cboNgayGiaoDich.Items.Add(String.Format("{0:00}", i));
                }
                cboNgayGiaoDich.SelectedValue = Session["NgayGiaoDich"].ToString();
            }
            catch
            {
                cboNgayGiaoDich.SelectedIndex = 0;
                Session["NgayGiaoDich"] = "01";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["MaTaiKhoan"] == null)
            {
                Session["RefUrl"] = "~/QuanLySoThuChi.aspx";
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                TaiKhoan taikhoan = Utilities.GetTaiKhoanById((int)Session["MaTaiKhoan"]);
                if (taikhoan == null)
                {
                    Session["RefUrl"] = "~/Test.aspx";
                    Response.Redirect("~/Login.aspx");
                }
            }
            if (!IsPostBack)
            {
                if (Session["cboLoaiPhieuIndex"] == null)
                    Session["cboLoaiPhieuIndex"] = 0;
                cboLoaiPhieu.SelectedIndex = (int)Session["cboLoaiPhieuIndex"];

                if (Session["MaKhoan"] == null)
                    Session["MaKhoan"] = 1;

                AddYearList();

                if (Session["Nam"] == null)
                    Session["Nam"] = DateTime.Today.Year.ToString();
                if (Session["Thang"] == null)
                    Session["Thang"] = String.Format("{0:00}", DateTime.Today.Month);
                if (Session["Ngay"] == null)
                    Session["Ngay"] = String.Format("{0:00}", DateTime.Today.Day);
                cboNam.SelectedValue = Session["Nam"].ToString();
                cboThang.SelectedValue = Session["Thang"].ToString();
                AddDayList();

                if (Session["NamGiaoDich"] == null)
                    Session["NamGiaoDich"] = DateTime.Today.Year.ToString();
                if (Session["ThangGiaoDich"] == null)
                    Session["ThangGiaoDich"] = String.Format("{0:00}", DateTime.Today.Month);
                if (Session["NgayGiaoDich"] == null)
                    Session["NgayGiaoDich"] = String.Format("{0:00}", DateTime.Today.Day);
                cboNamGiaoDich.SelectedValue = Session["NamGiaoDich"].ToString();
                cboThangGiaoDich.SelectedValue = Session["ThangGiaoDich"].ToString();
                AddDayGiaoDichList();
            }
        }

        protected void cboNam_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Nam"] = cboNam.SelectedValue;
            AddDayList();
        }

        protected void cboThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Thang"] = cboThang.SelectedValue;
            AddDayList();
        }

        protected void cboNgay_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Ngay"] = cboNgay.SelectedValue;
        }

        protected void cboNamGiaoDich_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["NamGiaoDich"] = cboNamGiaoDich.SelectedValue;
            AddDayGiaoDichList();
        }

        protected void cboThangGiaoDich_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["ThangGiaoDich"] = cboThangGiaoDich.SelectedValue;
            AddDayGiaoDichList();
        }

        protected void cboNgayGiaoDich_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["NgayGiaoDich"] = cboNgayGiaoDich.SelectedValue;
        }

        protected void LinqDataSourceSoThuChi_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            DCThuChiDataContext dc = new DCThuChiDataContext();
            var rs = from s in dc.SoThuChis
                     where s.Muc.MaTaiKhoan == (int)Session["MaTaiKhoan"]
                        && s.Muc.MaKhoan.ToString().Contains(cboLoaiPhieu.SelectedItem.Value)
                        && (s.NgayVaoSo.Day < 10 ? "0" + s.NgayVaoSo.Day : s.NgayVaoSo.Day.ToString()).Contains(cboNgay.SelectedItem.Value)
                        && (s.NgayVaoSo.Month < 10 ? "0" + s.NgayVaoSo.Month : s.NgayVaoSo.Month.ToString()).Contains(cboThang.SelectedItem.Value)
                        && (s.NgayVaoSo.Year < 10 ? "0" + s.NgayVaoSo.Year : s.NgayVaoSo.Year.ToString()).Contains(cboNam.SelectedItem.Value)
                     orderby s.STT descending
                     select s;
            e.Result = rs;
            decimal? tongthu = rs.Select(c => c.SoTienThu).Sum();
            decimal? tongchi = rs.Select(c => c.SoTienChi).Sum();
            lblTongThu.Text = "Tổng thu: " + String.Format("{0:0,0}",tongthu);
            lblTongChi.Text = "Tổng chi: " + String.Format("{0:0,0}",tongchi);
            lblTon.Text = "Tồn: " + String.Format("{0:0,0}", tongthu - tongchi);
            cboPageSize_SelectedIndexChanged(sender, e);
        }

        protected void cboLoaiPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["cboLoaiPhieuIndex"] = cboLoaiPhieu.SelectedIndex;
        }

        protected void cboPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvSoThuChi.PageSize = Convert.ToInt16(cboPageSize.SelectedValue);
        }

        protected void grvSoThuChi_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Trang " + (grvSoThuChi.PageIndex + 1) + " của " + grvSoThuChi.PageCount;
            }
        }

        protected void lbtnThemPhieuThu_Click(object sender, EventArgs e)
        {
            Session["MaKhoan"] = 1;
            //Session["MaMuc"] = null;
            lblSoThuChi.Text = "THÊM PHIẾU THU";
            btnCapNhat.Text = "Thêm";
            cboThangGiaoDich.Text = String.Format("{0:00}", DateTime.Today.Month);
            Session["NgayGiaoDich"] = String.Format("{0:00}", DateTime.Today.Day);
            cboThangGiaoDich_SelectedIndexChanged(sender, e);
            txtSoTien.ReadOnly = false;
            txtSoTien.Text = "1000";
            cboNgayGiaoDich.Focus();
            panCapNhatSoThuChi.Visible = true;
        }

        protected void lbtnThemPhieuChi_Click(object sender, EventArgs e)
        {
            Session["MaKhoan"] = 2;
            Session["MaMuc"] = null;
            btnCapNhat.Text = "Thêm";
            lblSoThuChi.Text = "THÊM PHIẾU CHI";
            cboThangGiaoDich.Text = String.Format("{0:00}", DateTime.Today.Month);
            Session["NgayGiaoDich"] = String.Format("{0:00}", DateTime.Today.Day);
            cboThangGiaoDich_SelectedIndexChanged(sender, e);
            txtSoTien.ReadOnly = false;
            txtSoTien.Text = "1000";
            cboNgayGiaoDich.Focus();
            panCapNhatSoThuChi.Visible = true;
        }

        private void ShowHide(TextBox txt, LinkButton lbtn1, LinkButton lbtn2, LinkButton lbtn3, LinkButton lbtn4, int _mode, String content)
        {
            lbtn1.Visible = lbtn2.Visible = !lbtn2.Visible;
            txt.Visible = lbtn3.Visible = lbtn4.Visible = !lbtn4.Visible;
            if (_mode == 0)
            {
                lbtn3.Text = "Thêm";
            }
            else if (_mode == 1)
            {
                lbtn3.Text = "Cập nhật";
            }
            txt.Text = content;
            txt.Focus();
        }

        protected void lbtnThemNhom_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenNhom, lbtnThemNhom, lbtnSuaNhom, lbtnDongYNhom, lbtnHuyBoNhom, 0, "");
        }

        protected void lbtnSuaNhom_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenNhom, lbtnThemNhom, lbtnSuaNhom, lbtnDongYNhom, lbtnHuyBoNhom, 1, cboNhom.SelectedItem.Text);
        }

        protected void lbtnThemDoiTac_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenDoiTac, lbtnThemDoiTac, lbtnSuaDoiTac, lbtnDongYDoiTac, lbtnHuyBoDoiTac, 0, "");
        }

        protected void lbtnSuaDoiTac_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenDoiTac, lbtnThemDoiTac, lbtnSuaDoiTac, lbtnDongYDoiTac, lbtnHuyBoDoiTac, 1, cboDoiTac.SelectedItem.Text);
        }

        protected void lbtnThemMuc_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenMuc, lbtnThemMuc, lbtnSuaMuc, lbtnDongYMuc, lbtnHuyBoMuc, 0, "");
        }

        protected void lbtnSuaMuc_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenMuc, lbtnThemMuc, lbtnSuaMuc, lbtnDongYMuc, lbtnHuyBoMuc, 1, cboMuc.SelectedItem.Text);
        }

        protected void lbtnDongYNhom_Click(object sender, EventArgs e)
        {
            DCThuChiDataContext dc = new DCThuChiDataContext();
            if (lbtnDongYNhom.Text == "Thêm")
            {
                Nhom nhom = new Nhom
                {
                    MaTaiKhoan = (int)Session["MaTaiKhoan"],
                    TenNhom = txtTenNhom.Text
                };
                dc.Nhoms.InsertOnSubmit(nhom);
                try
                {
                    dc.SubmitChanges();
                    cboNhom.DataBind();
                    cboNhom.Focus();
                    cboNhom.SelectedIndex = cboNhom.Items.Count - 1;
                    lblErrorSoThuChi.Text = "";
                }
                catch //(Exception ex)
                {
                    lblErrorSoThuChi.Text = "Lỗi khi tạo nhóm mới. Vui lòng thử lại!";
                }
            }
            else
            {
                Nhom nhom = dc.Nhoms.Single(n => n.MaNhom.ToString() == cboNhom.SelectedItem.Value);
                nhom.TenNhom = txtTenNhom.Text;
                try
                {
                    dc.SubmitChanges();
                    int id = cboNhom.SelectedIndex;
                    cboNhom.DataBind();
                    cboNhom.Focus();
                    cboNhom.SelectedIndex = id;
                    lblErrorSoThuChi.Text = "";
                }
                catch //(Exception ex)
                {
                    lblErrorSoThuChi.Text = "Lỗi khi sửa tên nhóm. Vui lòng thử lại!";
                }
            }
            ShowHide(txtTenNhom, lbtnThemNhom, lbtnSuaNhom, lbtnDongYNhom, lbtnHuyBoNhom, 2, txtTenNhom.Text);
        }

        protected void lbtnHuyBoNhom_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenNhom, lbtnThemNhom, lbtnSuaNhom, lbtnDongYNhom, lbtnHuyBoNhom, 3, "");
        }

        protected void lbtnDongYDoiTac_Click(object sender, EventArgs e)
        {
            DCThuChiDataContext dc = new DCThuChiDataContext();
            if (lbtnDongYDoiTac.Text == "Thêm")
            {
                DoiTac dt = new DoiTac
                {
                    MaNhom = Convert.ToInt32(cboNhom.Text),
                    TenDoiTac = txtTenDoiTac.Text
                };
                dc.DoiTacs.InsertOnSubmit(dt);
                try
                {
                    dc.SubmitChanges();
                    cboDoiTac.DataBind();
                    cboDoiTac.Focus();
                    cboDoiTac.SelectedIndex = cboDoiTac.Items.Count - 1;
                    lblErrorSoThuChi.Text = "";
                }
                catch //(Exception ex)
                {
                    lblErrorSoThuChi.Text = "Lỗi khi tạo đối tác mới. Vui lòng thử lại!";
                }
            }
            else
            {
                DoiTac dt = dc.DoiTacs.Single(d => d.MaDoiTac.ToString() == cboDoiTac.SelectedItem.Value);
                dt.TenDoiTac = txtTenDoiTac.Text;
                try
                {
                    dc.SubmitChanges();
                    int id = cboDoiTac.SelectedIndex;
                    cboDoiTac.DataBind();
                    cboDoiTac.Focus();
                    cboDoiTac.SelectedIndex = id;
                    lblErrorSoThuChi.Text = "";
                }
                catch //(Exception ex)
                {
                    lblErrorSoThuChi.Text = "Lỗi khi sửa tên đối tác. Vui lòng thử lại!";
                }
            }
            ShowHide(txtTenDoiTac, lbtnThemDoiTac, lbtnSuaDoiTac, lbtnDongYDoiTac, lbtnHuyBoDoiTac, 2, txtTenDoiTac.Text);
        }

        protected void lbtnHuyBoDoiTac_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenDoiTac, lbtnThemDoiTac, lbtnSuaDoiTac, lbtnDongYDoiTac, lbtnHuyBoDoiTac, 3, "");
        }

        protected void lbtnDongYMuc_Click(object sender, EventArgs e)
        {
            DCThuChiDataContext dc = new DCThuChiDataContext();
            if (lbtnDongYMuc.Text == "Thêm")
            {
                Muc muc = new Muc
                {
                    MaKhoan = (int)Session["MaKhoan"],
                    MaTaiKhoan = (int)Session["MaTaiKhoan"],
                    TenMuc = txtTenMuc.Text
                };
                dc.Mucs.InsertOnSubmit(muc);
                try
                {
                    dc.SubmitChanges();
                    cboMuc.DataBind();
                    cboMuc.Focus();
                    cboMuc.SelectedIndex = cboMuc.Items.Count - 1;
                    lblErrorSoThuChi.Text = "";
                }
                catch //(Exception ex)
                {
                    lblErrorSoThuChi.Text = "Lỗi khi tạo mục diễn giải mới. Vui lòng thử lại!";
                }
            }
            else
            {
                Muc muc = dc.Mucs.Single(m => m.MaMuc.ToString() == cboMuc.SelectedItem.Value);
                muc.TenMuc = txtTenMuc.Text;
                try
                {
                    dc.SubmitChanges();
                    int id = cboMuc.SelectedIndex;
                    cboMuc.DataBind();
                    cboMuc.Focus();
                    cboMuc.SelectedIndex = id;
                    lblErrorSoThuChi.Text = "";
                }
                catch //(Exception ex)
                {
                    lblErrorSoThuChi.Text = "Lỗi khi sửa tên mục diễn giải. Vui lòng thử lại!";
                }
            }
            ShowHide(txtTenMuc, lbtnThemMuc, lbtnSuaMuc, lbtnDongYMuc, lbtnHuyBoMuc, 2, txtTenMuc.Text);
        }

        protected void lbtnHuyBoMuc_Click(object sender, EventArgs e)
        {
            ShowHide(txtTenMuc, lbtnThemMuc, lbtnSuaMuc, lbtnDongYMuc, lbtnHuyBoMuc, 3, "");
        }

        protected void grvSoThuChi_SelectedIndexChanged(object sender, EventArgs e)
        {
            dem1 = 1;
            dem2 = 1;
            panCapNhatSoThuChi.Visible = true;
            btnCapNhat.Text = "Cập nhật";
            int rid = Math.Max(grvSoThuChi.SelectedIndex, 0);
            String maNhom;
            String maDoiTac;
            String maMuc;
            DateTime ngayGiaoDich;
            String soTien;
            String soPhieuThu;

            try
            {
                soTien = grvSoThuChi.DataKeys[rid].Values["SoTienThu"].ToString();
            }
            catch// (System.Exception ex)
            {
                soTien = grvSoThuChi.DataKeys[rid].Values["SoTienChi"].ToString();
            }

            txtSoTien.Text = String.Format("{0:0,0}", Convert.ToDouble(soTien));
            txtSoTien.ReadOnly = true;

            ngayGiaoDich = Convert.ToDateTime(grvSoThuChi.DataKeys[rid].Values["NgayGiaoDich"].ToString());
            Session["NgayGiaoDich"] = String.Format("{0:00}", ngayGiaoDich.Day);
            cboNamGiaoDich.Text = ngayGiaoDich.Year.ToString();
            cboThangGiaoDich.Text = String.Format("{0:00}", ngayGiaoDich.Month);
            cboThangGiaoDich_SelectedIndexChanged(sender, e);

            maDoiTac = grvSoThuChi.DataKeys[rid].Values["MaDoiTac"].ToString();
            DCThuChiDataContext dc = new DCThuChiDataContext();
            DoiTac dt = dc.DoiTacs.Single(d => d.MaDoiTac.ToString() == maDoiTac);
            maNhom = dt.MaNhom.ToString();
            cboNhom.SelectedValue = maNhom;
            Session["MaDoiTac"] = maDoiTac;

            try
            {
                soPhieuThu = grvSoThuChi.DataKeys[rid].Values["SoPhieuThu"].ToString();
            }
            catch //(System.Exception ex)
            {
                soPhieuThu = "";
            }


            if (soPhieuThu == "")
            {
                Session["MaKhoan"] = 2;
                lblSoThuChi.Text = "SỬA PHIẾU CHI";
            }
            else
            {
                Session["MaKhoan"] = 1;
                lblSoThuChi.Text = "SỬA PHIẾU THU";
            }

            maMuc = grvSoThuChi.DataKeys[rid].Values["MaMuc"].ToString();
            Session["MaMuc"] = maMuc;

            cboLoaiPhieu.Enabled = cboNgay.Enabled = cboThang.Enabled = cboNam.Enabled = cboPageSize.Enabled
                = lbtnThemPhieuThu.Enabled = lbtnThemPhieuChi.Enabled
                = lbtnThemPhieuThu0.Enabled = lbtnThemPhieuChi0.Enabled
                = false;
            cboNgayGiaoDich.Focus();
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            DCThuChiDataContext dc = new DCThuChiDataContext();
            if (btnCapNhat.Text == "Thêm")
            {
                try
                {
                    if (Session["MaKhoan"].ToString() == "1")
                    {
                        long? soPhieuThu = 0;
                        dc.InsertPhieuThu(ref soPhieuThu, Convert.ToInt32(cboDoiTac.Text),
                            Convert.ToInt32(cboMuc.Text),
                            new DateTime(
                                Convert.ToInt16(cboNamGiaoDich.Text),
                                Convert.ToInt16(cboThangGiaoDich.Text),
                                Convert.ToInt16(cboNgayGiaoDich.Text)),
                            Convert.ToDecimal(txtSoTien.Text));
                    }
                    else
                    {
                        long? soPhieuChi = 0;
                        dc.InsertPhieuChi(ref soPhieuChi, Convert.ToInt32(cboDoiTac.Text),
                            Convert.ToInt32(cboMuc.Text),
                            new DateTime(
                                Convert.ToInt16(cboNamGiaoDich.Text),
                                Convert.ToInt16(cboThangGiaoDich.Text),
                                Convert.ToInt16(cboNgayGiaoDich.Text)),
                            Convert.ToDecimal(txtSoTien.Text));
                    }
                    grvSoThuChi.DataBind();
                    lblErrorSoThuChi.Text = "";
                    btnHuyBo_Click(sender, e);
                }
                catch //(Exception ex)
                {
                    lblErrorSoThuChi.Text = "Lỗi khi thêm phiếu mới. Vui lòng thử lại!";
                }
            }
            else
            {
                int rid = grvSoThuChi.SelectedRow.RowIndex;
                QuanLySoThuChi stc = dc.SoThuChis.Single(id => id.STT.ToString() == grvSoThuChi.DataKeys[rid].Values["STT"].ToString());
                stc.NgayGiaoDich = new DateTime(
                                Convert.ToInt16(cboNamGiaoDich.Text),
                                Convert.ToInt16(cboThangGiaoDich.Text),
                                Convert.ToInt16(cboNgayGiaoDich.Text));
                stc.MaDoiTac = Convert.ToInt32(cboDoiTac.Text);
                stc.MaMuc = Convert.ToInt32(cboMuc.SelectedItem.Value);

                int maMucCu = Convert.ToInt32(grvSoThuChi.DataKeys[rid].Values["MaMuc"]);
                decimal soTien = Convert.ToDecimal(txtSoTien.Text);
                if (stc.MaMuc != maMucCu)
                {
                    Muc muc1 = dc.Mucs.Single(m => m.MaMuc == maMucCu);
                    Muc muc2 = dc.Mucs.Single(m => m.MaMuc == stc.MaMuc);
                    muc1.Ton = muc1.Ton - soTien;
                    muc2.Ton = muc2.Ton + soTien;
                }
                int maDoiTacCu = Convert.ToInt32(grvSoThuChi.DataKeys[rid].Values["MaDoiTac"]);
                if (stc.MaDoiTac != maDoiTacCu)
                {
                    DoiTac dt1 = dc.DoiTacs.Single(d => d.MaDoiTac == maDoiTacCu);
                    DoiTac dt2 = dc.DoiTacs.Single(d => d.MaDoiTac == stc.MaDoiTac);
                    Nhom n1 = dc.Nhoms.Single(n => n.MaNhom == dt1.MaNhom);
                    Nhom n2 = dc.Nhoms.Single(n => n.MaNhom == dt2.MaNhom);
                    if ((int)Session["MaKhoan"] == 1)
                    {
                        dt1.Ton = dt1.Ton + soTien;
                        dt2.Ton = dt2.Ton - soTien;
                        n1.Ton = n1.Ton + soTien;
                        n2.Ton = n2.Ton - soTien;
                    }
                    else
                    {
                        dt1.Ton = dt1.Ton - soTien;
                        dt2.Ton = dt2.Ton + soTien;
                        n1.Ton = n1.Ton - soTien;
                        n2.Ton = n2.Ton + soTien;
                    }
                }
                try
                {
                    dc.SubmitChanges();
                    grvSoThuChi.DataBind();
                    grvSoThuChi.SelectedIndex = rid;
                    lblErrorSoThuChi.Text = "";
                    btnHuyBo_Click(sender, e);
                }
                catch (Exception ex)
                {
                    lblErrorSoThuChi.Text = "Lỗi khi sửa phiếu. Vui lòng thử lại!" + "\n" + ex.Message.ToString();
                }
            }
        }

        protected void btnHuyBo_Click(object sender, EventArgs e)
        {
            if (btnCapNhat.Text == "Cập nhật")
            {
                cboLoaiPhieu.Enabled = cboNgay.Enabled = cboThang.Enabled 
                    = cboNam.Enabled = cboPageSize.Enabled
                    = lbtnThemPhieuThu.Enabled = lbtnThemPhieuThu0.Enabled
                    = lbtnThemPhieuChi.Enabled = lbtnThemPhieuChi0.Enabled
                    = true;
                grvSoThuChi.SelectedIndex = -1;
            }
            panCapNhatSoThuChi.Visible = false;
        }

        protected void cboMuc_PreRender(object sender, EventArgs e)
        {
            try
            {
                if (dem1 > 0)
                {
                    cboMuc.Text = Session["MaMuc"].ToString();
                    dem1--;
                }
            }
            catch //(System.Exception ex)
            {

            }
        }

        protected void cboDoiTac_PreRender(object sender, EventArgs e)
        {
            try
            {
                if (dem2 > 0)
                {
                    cboDoiTac.Text = Session["MaDoiTac"].ToString();
                    dem2--;
                }
            }
            catch //(System.Exception ex)
            {

            }
        }

        protected void cboNhom_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbtnDongYNhom.Text == "Cập nhật" && lbtnDongYNhom.Visible == true)
            {
                txtTenNhom.Text = cboNhom.SelectedItem.Text;
            }
        }

        protected void cboDoiTac_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbtnDongYDoiTac.Text == "Cập nhật" && lbtnDongYDoiTac.Visible == true)
            {
                txtTenDoiTac.Text = cboDoiTac.SelectedItem.Text;
            }
        }

        protected void cboMuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbtnDongYMuc.Text == "Cập nhật" && lbtnDongYMuc.Visible == true)
            {
                txtTenMuc.Text = cboMuc.SelectedItem.Text;
            }
        }

        protected void cboDoiTac_DataBound(object sender, EventArgs e)
        {
            if (lbtnDongYDoiTac.Text == "Cập nhật" && lbtnDongYDoiTac.Visible == true)
            {
                if (cboDoiTac.Items.Count > 0)
                {
                    txtTenDoiTac.Text = cboDoiTac.SelectedItem.Text;
                }
                else
                {
                    lbtnHuyBoDoiTac_Click(sender, e);
                }
            }
        }

        protected void grvSoThuChi_PreRender(object sender, EventArgs e)
        {
            grvSoThuChi.DataBind();
        }

        protected void grvSoThuChi_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (grvSoThuChi.SelectedIndex >-1 && e.CommandName == "Cancel")
            {
                btnHuyBo_Click(sender, e);
            }
        }

        protected void grvSoThuChi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if(btnCapNhat.Text=="Cập nhật")
                e.Cancel = panCapNhatSoThuChi.Visible;
            else
                e.Cancel = false;
        }
    }
}